package com.ruoyi.system.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.entity.CataloguePortTree;
import com.ruoyi.system.mapper.CataloguePortTreeMapper;
import com.ruoyi.system.service.CataloguePortTreeService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;

/**
 * 港口_文件管理目录树(CataloguePortTree)表服务实现类
 * @since 2023-04-19 17:56:44
 */
@Service("cataloguePortTreeService")
public class CataloguePortTreeServiceImpl implements CataloguePortTreeService {
    @Resource
    private CataloguePortTreeMapper cataloguePortTreeMapper;

    /**
     * 通过ID查询单条数据
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CataloguePortTree queryById(Integer id) {
        return this.cataloguePortTreeMapper.queryById(id);
    }

    /**
     * 分页查询
     * @return 查询结果
     */
    @Override
    public List<CataloguePortTree> queryByPage(CataloguePortTree cataloguePortTree) {
        return cataloguePortTreeMapper.queryAllByLimit(cataloguePortTree);
    }

    /**
     * 新增数据
     * @param cataloguePortTree 实例对象
     * @return 实例对象
     */
    @Override
    public CataloguePortTree insert(CataloguePortTree cataloguePortTree) {
        cataloguePortTree.setCreatedDate(DateUtils.getNowDate());
        cataloguePortTree.setYn(1);
        this.cataloguePortTreeMapper.insert(cataloguePortTree);
        return cataloguePortTree;
    }

    /**
     * 修改数据
     * @param cataloguePortTree 实例对象
     * @return 实例对象
     */
    @Override
    public CataloguePortTree update(CataloguePortTree cataloguePortTree) {
        this.cataloguePortTreeMapper.update(cataloguePortTree);
        return this.queryById(cataloguePortTree.getId());
    }

    /**
     * 通过主键删除数据
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.cataloguePortTreeMapper.deleteById(id) > 0;
    }


    /**
     * 树
     * @return
     */
    @Override
    public List<CataloguePortTree> tree() {
        CataloguePortTree cataloguePortTree = new CataloguePortTree();
        cataloguePortTree.setPid(0);
        List<CataloguePortTree> cataloguePortTreeList = cataloguePortTreeMapper.queryAll(cataloguePortTree);
        if (!CollectionUtils.isEmpty(cataloguePortTreeList)) {
            cataloguePortTreeList.forEach(value -> {
                CataloguePortTree portTree = new CataloguePortTree();
                portTree.setPid(value.getId());
                List<CataloguePortTree> hazardousTreeList = cataloguePortTreeMapper.queryAll(portTree);
                value.setTree(hazardousTreeList);
            });
        }
        return cataloguePortTreeList;
    }


}
